export default class IconKObj extends godot.KinematicBody2D {
    v = new godot.Vector2()
    isMove = false;

    constructor(x = 200,y=200,img="",isMove=true){
        super();
        // 设置位置
        this.position = new godot.Vector2(x,y);
        this.isMove = isMove;

        // 设置外观
        let spr = new godot.Sprite();
        if(img){
            spr.texture = godot.load(img) as godot.Texture;
        }else{
            spr.texture = godot.load("res://assets/coin.png") as godot.Texture;
        }
        this.add_child(spr);

        // 设置包围盒
        let shape = new godot.RectangleShape2D();
        let w = spr.texture.get_width();
        let h = spr.texture.get_height();
        shape.extents = new godot.Vector2(w/2,h/2);
        let oid = this.create_shape_owner(this);
        this.shape_owner_add_shape(oid,shape);
    }

    // Kobj碰撞相应
    _physics_process(delta: number): void {
        if(this.isMove){
            this.handleInput();

            // move_and_collide() 移动碰撞体并进行碰撞检测
            let res = this.move_and_collide(this.v);
            if(res){
                console.log(new Date(),"Kobj碰撞");
            }
        }
    }   

    handleInput(){
        this.v = new godot.Vector2()
        let d = 3
        if(godot.Input.is_key_pressed(godot.KEY_D)){
            this.v.x = d
        }
        if(godot.Input.is_key_pressed(godot.KEY_A)){
            this.v.x = -d
        }   
        if(godot.Input.is_key_pressed(godot.KEY_W)){
            this.v.y = -d
        }
        if(godot.Input.is_key_pressed(godot.KEY_S)){
            this.v.y = d
        }   
    }
}